﻿using System;
using System.Text;
using System.Web;

using Cognifide.PowerShell.Shell;
using Cognifide.PowerShell.Shell.Host;
using Cognifide.PowerShell.Shell.Settings;

using Sitecore;
using Sitecore.Shell.Framework.Commands;

namespace Cognifide.PowerShell.Console.Controls
{
    public partial class ScriptExecution : System.Web.UI.UserControl
    {

        public string Script { get; set; }
        public bool NonInteractive { get; set; }
        public const string VERSION_STRING = "Sitecore Powershell Console\r\nCopyright (c) 2010-2011 <a href='http://www.cognifide.com' target='_blank'>Cognifide Limited</a> & <a href='http://blog.najmanowicz.com/' target='_blank'>Adam Najmanowicz</a>. \nAll rights Reserved\r\n\r\nEnter your script/command and press Ctrl+Enter to execute your script.\n\n";
        public const string VERSION_STRING_RESULTS = "Sitecore Powershell Console\r\nCopyright (c) 2010-2011 <a href='http://www.cognifide.com' target='_blank'>Cognifide Limited</a> & <a href='http://blog.najmanowicz.com/' target='_blank'>Adam Najmanowicz</a>. \r\n\r\n";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!NonInteractive)
            {
                litOutput.Text = VERSION_STRING;
            }
            else
            {
                plhOutput.Attributes["class"] = "consoleWrapper";
            }
        }

        protected string BackgroundColor
        {
            get
            {
                    return PsConsoleSettings.Instance.BackgroundColor.ToString().Replace("DarkBlue", "#012456");
            }
        }

        protected string ForegroundColor
        {
            get
            {
                return PsConsoleSettings.Instance.ForegroundColor.ToString().Replace("DarkBlue", "#012456");
            }
        }

        public void Execute()
        {
            litVars.Text = String.Empty;
            litErrors.Text = String.Empty;
            litOutput.Text = String.Empty;
            litExceptions.Text = String.Empty;
            using (var scriptSession = new ScriptSession())
            {
                try
                {
                    if (!NonInteractive)
                    {
                        scriptSession.ExecuteScriptPart(PsConsoleSettings.Instance.PreScript);
                    }
                    scriptSession.ExecuteScriptPart(Script);
                }
                catch (Exception exc)
                {
                    litExceptions.Text = scriptSession.GetExceptionString(exc);
                }
                
                StringBuilder output = new StringBuilder(10240);
                foreach (var outputLine in scriptSession.Output)
                {
                    outputLine.GetHtmlLine(output);
                }
                litOutput.Text += (NonInteractive ? VERSION_STRING_RESULTS :  VERSION_STRING) + output.ToString();
            }

            plhProblems.Visible = !String.IsNullOrEmpty(litExceptions.Text) || !String.IsNullOrEmpty(litErrors.Text);
            plhException.Visible = !String.IsNullOrEmpty(litExceptions.Text);
            plhErrors.Visible = !String.IsNullOrEmpty(litErrors.Text);
        }

    }
}
